﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Garlic.CC.BaseClass;
using Garlic.BLL;
using Garlic.DM.DataModel;
using System.Collections.Generic;
using Garlic.CC.WebUtility;
using Garlic.CC.Utility;

public partial class SystemManagement_SetRoleForMenu : BasePage
{
    

    private List<RoleInfo> forMenuRoles;

    #region page load
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            //WorkshopMgr = new WorkshopManager(this.CurrentUser);
            if (!Page.IsPostBack)
            {
                //Init Page
                //   InitSearchBar();

                string menuID = Request.QueryString["MenuID"];
                if (string.IsNullOrEmpty(menuID))
                    throw new Exception();

                MenuInfo menu = MenuManager.GetInfo(int.Parse(menuID));

                menuName.InnerText = menu.Menu_name;
                hdMenuID.Value = menuID;

                this.Search();
            }
        }
        catch (Exception ex)
        {
            this.HandleException(ex);
        }
    }

    #endregion

    #region method
    /// <summary>
    /// 初始化页面
    /// </summary>
    private void InitPage()
    {

    }

    /// <summary>
    /// 初始化查询栏
    /// </summary>
    private void InitSearchBar()
    {
        //初始化查询列表
        // this.GeneralSearchBar1.ListSourcXYbles = WorkshopMgr.GetSearchBarDataSourcXYbles();


    }

    private void SaveRecords()
    {
        List<int> checkedRecords = new List<int>();
        List<int> allRecords = new List<int>();
        for (int i = 0; i < this.SmartGrid.Rows.Count; i++)
        {
            CheckBox cbSelected = this.SmartGrid.Rows[i].FindControl("checkitem") as CheckBox;
            if (cbSelected != null)
            {
                if (cbSelected.Checked)
                    checkedRecords.Add(int.Parse(SmartGrid.DataKeys[i].Value.ToString()));
                allRecords.Add(int.Parse(SmartGrid.DataKeys[i].Value.ToString()));
            }
        }
        //if (checkedRecords.Count == 0) return;

        int menuID = int.Parse(hdMenuID.Value);

        foreach (int roleID in allRecords)
        {
            MenuManager.RemoveToRole(menuID, roleID);
        }

        foreach (int roleID in checkedRecords)
        {
            MenuManager.AddToRole(menuID, roleID);
        }

        this.ShowMessage("成功。");
    }

    private void Search()
    {
        //string condition = this.GeneralSearchBar1.GetConditionStr();
        //this.SmartDataViewSource.SelectParameters["textWhere"].DefaultValue = condition;
        string condition = "";

        if (searchRoleName.Text.Trim() != "")
            condition += " role_name like '%" + SqlHelper.ReplaceInvalidWords(searchRoleName.Text) + "%'";
        if (searchRoleType.SelectedValue != "-1")
        {
            if (condition != "")
                condition += " and ";
            condition += " role_type =" + searchRoleType.SelectedValue;
        }
        this.SmartDataViewSource.SelectParameters["textWhere"].DefaultValue = condition;
    }

    private void ShowMessage(string msg)
    {
        this.Message = msg;
    }

    #endregion

    #region toolbar event handler
    /// <summary>
    /// 工具栏事件处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="args"></param>
    protected void TopToolBar_OnClick(object sender, Garlic.UI.Components.IconButtonClickEventArgs args)
    {
            switch (args.Command)
            {
                case ToolbarCommand.Save:
                    SaveRecords();
                    break;
                case ToolbarCommand.Search:
                    Search();
                    break;
                case ToolbarCommand.HideOrExpand:
                    this.divSearch.Visible = !this.divSearch.Visible;
                    break;
            }
        
    }
    #endregion

    protected void SmartGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[2].Text == "0")
                    e.Row.Cells[2].Text = "普通用戶";
                else
                    e.Row.Cells[2].Text = "&nbsp;";

                if (forMenuRoles == null)
                {
                    forMenuRoles = RoleManager.GetRolesByMenuID(int.Parse(hdMenuID.Value));
                }

                foreach (RoleInfo role in forMenuRoles)
                {
                    if (int.Parse(SmartGrid.DataKeys[e.Row.DataItemIndex - SmartGrid.PageSize * (SmartGrid.PageIndex)].Value.ToString()) == role.Role_id)
                    {
                        //CheckBox cbSelected = this.SmartGrid.Rows[e.Row.DataItemIndex].FindControl("checkitem") as CheckBox;
                        CheckBox cbSelected = e.Row.FindControl("checkitem") as CheckBox;
                        if (cbSelected != null)
                        {
                            cbSelected.Checked = true;
                        }
                        break;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            this.HandleException(ex);
        }
    }
}
